<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册页面</title>
    <link rel="stylesheet" href="/css/register.css">
    <script src="/script/SHA256.js"></script>
</head>
<body>
    <div class="tip">
        <p>{{.}}</p> <!--  @TODO 修改回显位置-->
    </div>
    <!-- action="https://404060p9q5.zicp.fun/register" -->
    <form method="post" action="#">
        <div id="register-box">
            <span>欢迎注册</span>
            <div class="tips">
                <label class="form-label" id="tips">已有账号？<a href="https://404060p9q5.zicp.fun/loginIndex">点击登录</a></label><br>
                <!-- @TODO 记得修改公网地址-->
            </div>
            <div class="form">
                <div class="radio-inline">
                    <label class="form-label">请选择您的身份</label>
                    <label class="idRadio">
                        <input type="radio" name="idRadioOption" id="voter" required><label for="voter">投票人</label>
                    </label>
                    <label class="idRadio">
                        <input type="radio" name="idRadioOption" id="candidate" required><label for="candidate">候选人</label>
                    </label>
                    <label class="idRadio">
                        <input type="radio" name="idRadioOption" id="notary" required><label for="notary">公证人</label>
                    </label>
                </div>
                <div class="item">
                    <label class="form-label" for="name">昵称</label>
                    <input name="nickname" id="name" class="form-input" type="text" placeholder="请输入昵称"required/><br>
                </div>
                <div class="item">
                    <label class="form-label" for="email">邮箱</label>
                    <input name="email" id="email" class="form-input" type="email" placeholder="请输入邮箱" required/><br>
                </div>
                <div class="item">
                    <label class="form-label" for="password">密码</label>
                    <input name="password" id="password" class="form-input" type="password" placeholder="请输入密码" required/><br>
                </div>
                <div class="item">
                    <label class="form-label" for="verificationcode">验证码</label>
                    <input name="verifyCode" id="verificationcode" class="form-input" type="text" placeholder="请输入验证码" />
                </div>
                <input id="btn" type="submit" value="注册" onclick="getForm()">
            </div>
        </div>
    </form>
    <a id="code" onclick="getVerifyCode()">
        <button>获取验证码</button>
    </a>
  
    <script>
        function getVerifyCode(){
            const mail = document.getElementById("email").value;
            const url = "https://404060p9q5.zicp.fun/sendVerifyCode?mail=" + mail// @TODO 修改公网地址
            alert(url)
            const http = new XMLHttpRequest()
            http.open("GET", url, true)
            http.send()
        }
    </script>

    <script>
        function getForm(){
            const name = document.getElementById("name").value;
            const email = document.getElementById("email").value;
            const password = document.getElementById("password").value;
            const verificationcode = document.getElementById("verificationcode").value;
            const idRadioOption = document.getElementsByName("idRadioOption")
            var identity = 4
            for (i = 0; i < idRadioOption.length; i++){
                if (idRadioOption[i].checked) {
                    identity = i
                    console.log("Res:", idRadioOption[i].checked)
                }
            }
            console.log("Identity:", identity)

            const shaPwd = SHA256(password)
            console.log(shaPwd)

            var http = new XMLHttpRequest();
            var url = 'https://404060p9q5.zicp.fun/register';
            var params = 'name=' + name + '&' + 'email=' + email + '&' + 'password=' + shaPwd +'&' + 'verificationcode=' + verificationcode + '&' + 'idRadioOption=' + identity;
            http.open('POST', url, true);
            //Send the proper header information along with the request
            http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            http.send(params);
        }
    </script>
</body>
</html>